#include "Score.h"
//#include "Global.h"
#include "Resource.h"

Score::Score(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y) : Object(x, y)
{
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\score.png"), 0, 0);
	
	score = 0;

	_xOld = x;
	_yOld = y;

	veloc_x = 0;
	veloc_y = 1.5;//-200;
	accel_y = GRAVITY;

	obj_state = IS_MOVING;
}

Score::~Score()
{
	/*if (_sprite != NULL)
	{
		delete _sprite;
	}*/
}

 

void Score::Render(RECT camera, int cameraX, int cameraY)
{
	DrawNumber(p_sprite, score, camera.left + pos_x, pos_y, cameraX, cameraY);
}

void Score::Render(int cameraX, int cameraY)
{
	DrawNumber(p_sprite, score, pos_x, pos_y, cameraX, cameraY);
}

OBJECT_TYPE Score::getObjectType()
{
	return OBJECT_LAND;
}

//void Score::ResetRect()
//{
//	_rectBond.left = _x;
//	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
//	_rectBond.top = _y;
//	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
//}

void Score::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	  
	veloc_y -= accel_y * gameTime;
	pos_y += veloc_y * gameTime + 1/2 * accel_y * gameTime * gameTime;
	if (veloc_y <= 0)//_y > _yOld)
	{
		changeState(IS_IDLE);
	}
	p_sprite->update(gameTime);
	//ResetRect();
	 
}

void Score::DrawNumber(Sprite* sprite, int number, int x, int y, int cameraX, int cameraY)
{
	RECT rSrc;
	int numWidth=p_sprite->getTexture()->getWidth();///10;
	do
	{
		int temp=number%10;
		number=number/10;
		sprite->setIndex(temp);//_Index = temp;
		/*rSrc.left=numWidth*temp;
		rSrc.right=numWidth*(temp+1);
		rSrc.top=0;
		rSrc.bottom=sprite->texture->_Height;*/
		//sprite->Draw(x,y,zoomX,zoomY,rSrc,0.1);
		sprite->render(x, y, cameraX, cameraY);
		x = x - numWidth;
	}while(number>0);
}

void Score::Update(int Score)
{
	 
	this->score = Score;
}

void Score::changeState(int state)
{
	obj_state = state;
	switch(obj_state)
	{	
	case IS_MOVING:
		veloc_y = 2.5;
		accel_y = GRAVITY;
		break;

	case IS_IDLE:
		break;
	}
}